package com.mysite.seckill.web.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysite.seckill.commons.exception.BusinessError;
import com.mysite.seckill.commons.exception.BusinessException;
import com.mysite.seckill.commons.vo.ResultVo;

@ControllerAdvice
public class BusinessControllerAdvice {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 全局异常捕捉处理
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResultVo errorHandler(Exception e) {
        logger.error(e.toString());
        return new ResultVo(BusinessError.COMMON_ERROR);
    }

    /**
     * 拦截捕捉自定义异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public ResultVo myErrorHandler(BusinessException e) {
        logger.error(e.toString());
        return new ResultVo(e.getError());
    }
}
